<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button class="filter-item" size="small" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate(0)">
        添加
      </el-button>
    </div>
    <el-table
      ref="roleTable"
      v-loading="listLoading"
      :data="menuList"
      element-loading-text="Loading"
      row-key="id"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="菜单名称">
        <template slot-scope="scope">
          <span>{{ scope.row.title }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 0">
            <el-tag>目录</el-tag>
          </span>
          <span v-if="scope.row.type === 1">
            <el-tag type="success">菜单</el-tag>
          </span>
          <span v-if="scope.row.type === 2">
            <el-tag type="info">按钮</el-tag>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="图标" align="center">
        <template slot-scope="scope">
          <span><i :class="'el-icon-' + scope.row.icon"></i></span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.sort }}</span>
        </template>
      </el-table-column>
      <el-table-column label="路径" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.path }}</span>
        </template>
      </el-table-column>
      <el-table-column label="权限标识" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.perms }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('table.actions')" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" type="primary" @click="handleCreate(row.id)">
            添加
          </el-button>
          <el-button size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button size="mini" type="danger" @click="deleteData(row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加/编辑 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="center" label-width="80px">
        <el-form-item size="medium" label="菜单类型">
          <el-radio-group v-model="temp.type" size="mini">
            <el-radio-button :label="0">目录</el-radio-button>
            <el-radio-button :label="2">按钮</el-radio-button>
            <el-radio-button :label="1">菜单</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="temp.type !== 2" size="medium" label="图标">
          <el-input v-model="temp.icon"/>
        </el-form-item>
        <el-form-item size="medium" label="名称" prop="title">
          <el-input v-model="temp.title"/>
        </el-form-item>
        <el-form-item size="medium" label="上级菜单">
          <treeselect v-model="temp.parentId" :options="menuTree" :normalizer="normalizer"/>
        </el-form-item>
        <el-form-item v-if="temp.type === 2" size="medium" label="权限标识" prop="perms">
          <el-input v-model="temp.perms"/>
        </el-form-item>
        <el-form-item v-if="temp.type === 1" size="medium" label="内部菜单">
          <el-radio-group v-model="menuType" size="mini">
            <el-radio-button :label="1">是</el-radio-button>
            <el-radio-button :label="2">否</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="menuType === 1 && temp.type !== 2" size="medium" label="菜单路由" prop="path">
          <el-input v-model="temp.path"/>
        </el-form-item>
        <el-form-item v-else-if="temp.type !== 2 && menuType === 2" size="medium" label="外部链接" prop="redirect">
          <el-input v-model="temp.redirect"/>
        </el-form-item>
        <el-form-item size="medium" label="排序" prop="sort">
          <el-input-number v-model="temp.sort" size="medium" :min="0" label="描述文字"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus === 'create'? createData() : updateData() ">
          提交
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  import { getMenus, addMenus, updateMenus, deleteMenus } from '@/api/menu'

  export default {
    name: 'Menu',
    components: {
      Treeselect
    },
    data() {
      return {
        listLoading: true,
        menuList: [],
        menuTree: [],
        menusProps: {
          children: 'children'
        },
        temp: {
          id: undefined,
          icon: '',
          title: '',
          parentId: 0,
          perms: '',
          component: '',
          redirect: '',
          type: 0,
          sort: 999,
          path: '',
          name: ''
        },
        menuType: 1,
        dialogStatus: '',
        dialogFormVisible: false,
        textMap: {
          update: '编辑',
          create: '添加'
        },
        rules: {
          title: [{ required: true, message: '请输入名称', trigger: 'blur' }],
          sort: [{ required: true, message: '请输入权限标识', trigger: 'blur' }],
          path: [{ required: true, message: '请输入组件路径', trigger: 'blur' }],
          perms: [{ required: true, message: '请输入权限标识', trigger: 'blur' }],
          redirect: [{ required: true, message: '请输入外部链接', trigger: 'blur' }]
        }
      }
    },
    created() {
      this.getMenuList()
    },
    methods: {
      // 重置表单
      resetTemp() {
        this.temp = {
          id: undefined,
          icon: '',
          title: '',
          parentId: 0,
          perms: '',
          component: '',
          redirect: '',
          type: 0,
          sort: 999,
          path: '',
          name: ''
        }
      },
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children
        }
        return {
          id: node.id,
          label: node.title,
          children: node.children
        }
      },
      getMenuList() {
        this.listLoading = true
        getMenus().then(({ data }) => {
          this.menuList = data
          this.listLoading = false
        }).catch(error => {
          console.log(error)
        })
      },
      handleCreate(id) {
        this.resetTemp()
        this.addRootTree()
        this.temp.parentId = id
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      // 弹出框
      handleUpdate(row) {
        // this.deptTree()
        this.temp = Object.assign({}, row) // copy obj
        this.addRootTree()
        // console.log(this.menuTree)
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      addRootTree() {
        this.menuTree = [
          {
            title: '根节点',
            id: 0,
            children: this.menuList
            // parentId: -1
          }
        ]
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            addMenus(this.temp).then(() => {
              this.$notify({
                title: '成功',
                message: '添加成功',
                type: 'success',
                duration: 2000
              })
              this.dialogFormVisible = false
              this.getMenuList()
            }).catch((error) => {
              this.$notify({
                title: '失败',
                message: '添加失败' + error,
                type: 'error',
                duration: 2000
              })
            })
          }
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            updateMenus(this.temp).then(() => {
              this.$notify({
                title: '成功',
                message: '添加成功',
                type: 'success',
                duration: 2000
              })
              this.dialogFormVisible = false
              this.getMenuList()
            }).catch((error) => {
              this.$notify({
                title: '失败',
                message: '添加失败' + error,
                type: 'error',
                duration: 2000
              })
            })
          }
        })
      },
      deleteData(id) {
        this.$confirm('您确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteMenus(id).then(() => {
            this.$notify.success({
              title: '成功',
              message: '删除成功'
            })
            this.getMenuList()
          }).catch(() => {
            this.$notify.error({
              title: '错误',
              message: '删除失败'
            })
          })
        })
      }
    }
  }
</script>

<style scoped>

</style>
